package hot;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Codec {
    public String serialize(TreeNode root) {
        return preTrave(root,"");
    }
    public String preTrave(TreeNode root,String str) {
        if(root == null) {
            str += "null,";
        }else {
            str += str.valueOf(root.val) +",";
            str = preTrave(root.left,str);
            str = preTrave(root.right,str);
        }
        return str;
    }
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String[] dataArray = data.split(",");
        List<String> dataList = new LinkedList<String>(Arrays.asList(dataArray));
        return rd(dataList);
    }
    public TreeNode rd(List<String> dataList) {
        if(dataList.get(0).equals("null")) {
            dataList.remove(0);
            return null;
        }

            TreeNode node = new TreeNode(Integer.valueOf(dataList.get(0)));
            dataList.remove(0);
            node.left = rd(dataList);
            node.right = rd(dataList);
            return node;
    }
}
